/*
-----------------------------------------------------------------------------
Filename:    BasicTutorial3.cpp
-----------------------------------------------------------------------------
 
This source file is part of the
   ___                 __    __ _ _    _ 
  /___\__ _ _ __ ___  / / /\ \ (_) | _(_)
 //  // _` | '__/ _ \ \ \/  \/ / | |/ / |
/ \_// (_| | | |  __/  \  /\  /| |   <| |
\___/ \__, |_|  \___|   \/  \/ |_|_|\_\_|
      |___/                              
      Tutorial Framework
      http://www.ogre3d.org/tikiwiki/
-----------------------------------------------------------------------------
*/
 
#include "TutorialApplication.h"
 
//-------------------------------------------------------------------------------------
BasicTutorial::BasicTutorial(void) {
}
//-------------------------------------------------------------------------------------
BasicTutorial::~BasicTutorial(void) { 
}
//-------------------------------------------------------------------------------------
void BasicTutorial::createScene(void) {
    mSceneMgr->setAmbientLight(Ogre::ColourValue(.25, .25, .25));

    Ogre::Entity *ninjaEntity = mSceneMgr->createEntity("Ninja", "ninja.mesh");
    Ogre::SceneNode *ninjaNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("NinjaNode");
    ninjaNode->attachObject(ninjaEntity);

    Ogre::Light *pointLight = mSceneMgr->createLight("pointLight");
    pointLight->setType(Ogre::Light::LT_POINT);
    pointLight->setPosition(Ogre::Vector3(250, 150, 250));
    pointLight->setDiffuseColour(Ogre::ColourValue::White);
    pointLight->setSpecularColour(Ogre::ColourValue::White);

}

bool BasicTutorial::frameRenderingQueued(const Ogre::FrameEvent& evt) {
    bool ret = MyParent::frameRenderingQueued(evt);
    if (!processUnbufferedInput(evt))
        return false;
    return ret;
}

bool BasicTutorial::processUnbufferedInput(const Ogre::FrameEvent &evt) {
    static bool mMouseDown = false;
    static Ogre::Real mToggle = 0.0;
    const Ogre::Real mRotate = 0.13;
    const Ogre::Real mMove = 250;

    const bool currMouse = mMouse->getMouseState().buttonDown(OIS::MB_Left);
    if (currMouse && !mMouseDown) {
        Ogre::Light *light = mSceneMgr->getLight("pointLight");
        light->setVisible(!light->isVisible());
    }
    mMouseDown = currMouse;

    mToggle -= evt.timeSinceLastFrame;
    if (mToggle < 0.0f && mKeyboard->isKeyDown(OIS::KC_1)) {
        mToggle = 0.5f;
        Ogre::Light *light = mSceneMgr->getLight("pointLight");
        light->setVisible(!light->isVisible());
    }

    Ogre::Vector3 transVector = Ogre::Vector3::ZERO;
    if (mKeyboard->isKeyDown(OIS::KC_I))
        transVector.z -= mMove;
    if (mKeyboard->isKeyDown(OIS::KC_K))
        transVector.z += mMove;
    if (mKeyboard->isKeyDown(OIS::KC_J)) {
        if (mKeyboard->isKeyDown(OIS::KC_LSHIFT))
            mSceneMgr->getSceneNode("NinjaNode")->yaw(Ogre::Degree(mRotate * 5));
        else
            transVector.x -= mMove;
    }
    if (mKeyboard->isKeyDown(OIS::KC_L)) {
        if (mKeyboard->isKeyDown(OIS::KC_LSHIFT))
            mSceneMgr->getSceneNode("NinjaNode")->yaw(Ogre::Degree(-mRotate * 5));
        else
            transVector.x += mMove;
    }
    if (mKeyboard->isKeyDown(OIS::KC_U))
        transVector.y += mMove;
    if (mKeyboard->isKeyDown(OIS::KC_O))
        transVector.y -= mMove;

    mSceneMgr->getSceneNode("NinjaNode")->translate(transVector * evt.timeSinceLastFrame, Ogre::Node::TS_LOCAL);

    return true;
}
 
 
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif
 
#ifdef __cplusplus
extern "C" {
#endif
 
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
    int main(int argc, char *argv[])
#endif
    {
        // Create application object
        BasicTutorial app;
 
        try {
            app.go();
        } catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
            MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
            std::cerr << "An exception has occured: " <<
                e.getFullDescription().c_str() << std::endl;
#endif
        }
 
        return 0;
    }
 
#ifdef __cplusplus
}
#endif
